package com.olddriver.crm.web.controller;

import com.olddriver.crm.domain.Dictionary;
import com.olddriver.crm.page.PageResult;
import com.olddriver.crm.query.QueryObject;
import com.olddriver.crm.service.IDictionaryService;
import com.olddriver.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("dictionary")
public class DictionaryController {
    @Autowired
    private IDictionaryService dictionaryService;

    @RequestMapping("list")
    public String list() {
        return "dictionary/list";
    }

    @RequestMapping("queryMenu")
    @ResponseBody
    public List<Dictionary> queryMenu() {
        return dictionaryService.queryMenu();
    }

    @RequestMapping("queryWords")
    @ResponseBody
    public PageResult queryWords(QueryObject qo,Long id){
        return dictionaryService.queryWords(qo,id);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Dictionary dictionary){
        Long id = dictionary.getId();
        try {
            if (id != null) {
                dictionaryService.update(dictionary);
            } else {
                dictionaryService.insert(dictionary);
            }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }

    @RequestMapping("deleteWord")
    @ResponseBody
    public JsonResult deleteWord(Long id){
        dictionaryService.deleteWord(id);
        return JsonResult.success();
    }
    @RequestMapping("deleteMenu")
    @ResponseBody
    public JsonResult delete(Long id){
        dictionaryService.deleteMenu(id);
        return JsonResult.success();
    }

    @RequestMapping("getWords")
    @ResponseBody
    public List<Dictionary> getWords(String sn){
        List<Dictionary> words = dictionaryService.getWords(sn);
        return words;
    }

}
